package com.mrper.ext.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Mrper on 16-3-13.
 * 模板类型数据适配器
 */
@SuppressWarnings("all")
public class TempleteAdapter<B extends ViewDataBinding,E> extends BaseAdapter {

    public interface OnInitAdapterListener<B extends ViewDataBinding,E>{
        void initSetValue(B binder,E item,int position);
    }

    protected Context context;
    protected LayoutInflater inflater;
    protected List<E> source;
    protected int layoutId;
    protected OnInitAdapterListener<B,E> initAdapterListener;

    public TempleteAdapter(Context context, int layoutId,OnInitAdapterListener<B,E> listener){
        this(context,layoutId,null,listener);
    }

    public TempleteAdapter(Context context, int layoutId,List<E> source){
        this(context,layoutId,source,null);
    }

    public TempleteAdapter(Context context, int layoutId,List<E> source,OnInitAdapterListener<B,E> listener){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.layoutId = layoutId;
        this.source = source == null?new ArrayList<E>():source;
        this.initAdapterListener = listener;
    }

    public void setOnInitAdapterListener(OnInitAdapterListener<B,E> listener){
        this.initAdapterListener = listener;
    }

    public int getLayoutId(){
        return this.layoutId;
    }

    /**
     * 添加Item项
     * @param item 数据集合
     */
    @SuppressWarnings({"unchecked","varargs"})
    public synchronized void addItem(E... item) {
        addItem(false,item);
    }

    /**
     * 添加Item项
     * @param isClear 是否清空原数据
     * @param item 数据集合
     */
    @SuppressWarnings({"unchecked","varargs"})
    public synchronized void addItem(boolean isClear,E... item){
        if(isClear) source.clear();
        if(item!=null && item.length>0)
            source.addAll(Arrays.asList(item));
    }

    /**
     * 添加列表项
     * @param items 列表项集合
     */
    public synchronized void addItem(List<E> items){
        addItem(items,false);
    }

    /**
     * 添加列表项
     * @param items 列表项集合
     * @param isClear 是否清空原数据
     */
    public synchronized void addItem(List<E> items,boolean isClear){
        if(isClear) source.clear();
        if(items!=null && items.size()>0)
            source.addAll(items);
    }

    /**
     * 添加一项数据
     * @param position 索引
     * @param item 数据项
     */
    public synchronized void addItem(int position,E item){
        if(item!=null && position >= 0)
            source.add(position, item);
    }

    /**
     * 删除一项数据
     * @param item 数据对象
     */
    public synchronized void removeItem(E item){
        if(item!=null) source.remove(item);
    }

    /**
     * 删除某个位置的数据
     * @param position 索引
     */
    public synchronized void removeAt(int position){
        source.remove(position);
    }

    /**  清空数据  */
    public synchronized void clear(){
        source.clear();
    }

    /**  同步刷新数据   */
    public void syncDataSource(){
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return source == null?0:source.size();
    }

    @Override
    public Object getItem(int position) {
        return source.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    @SuppressWarnings("unchecked")
    public View getView(int position, View convertView, ViewGroup parent) {
        final B binding;
        if(convertView == null){
            binding = DataBindingUtil.inflate(inflater,layoutId,null,false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        }else{
            binding = (B)convertView.getTag();
        }
        if(initAdapterListener!=null)
            initAdapterListener.initSetValue(binding,source.get(position),position);
        binding.executePendingBindings();
        return convertView;
    }

}
